package com.ruoyi.voucher.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.voucher.domain.VoucherWord;
import com.ruoyi.voucher.mapper.VoucherWordMapper;
import com.ruoyi.voucher.service.IVoucherWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 凭证字Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@Service
public class VoucherWordServiceImpl extends ServiceImpl<VoucherWordMapper,VoucherWord> implements IVoucherWordService
{
    @Autowired
    private VoucherWordMapper voucherWordMapper;

    /**
     * 查询凭证字
     * 
     * @param id 凭证字主键
     * @return 凭证字
     */
    @Override
    public VoucherWord selectVoucherWordById(Long id)
    {
        return voucherWordMapper.selectVoucherWordById(id);
    }

    /**
     * 查询凭证字列表
     * 
     * @param voucherWord 凭证字
     * @return 凭证字
     */
    @Override
    public List<VoucherWord> selectVoucherWordList(VoucherWord voucherWord)
    {
        return voucherWordMapper.selectVoucherWordList(voucherWord);
    }

    /**
     * 新增凭证字
     * 
     * @param voucherWord 凭证字
     * @return 结果
     */
    @Override
    public int insertVoucherWord(VoucherWord voucherWord)
    {
        return voucherWordMapper.insert(voucherWord);
    }

    /**
     * 修改凭证字
     * 
     * @param voucherWord 凭证字
     * @return 结果
     */
    @Override
    public int updateVoucherWord(VoucherWord voucherWord)
    {
        return voucherWordMapper.updateById(voucherWord);
    }

    /**
     * 批量删除凭证字
     * 
     * @param ids 需要删除的凭证字主键
     * @return 结果
     */
    @Override
    public int deleteVoucherWordByIds(Long[] ids) throws Exception {
        for (Long id : ids) {
            VoucherWord voucherWord = voucherWordMapper.selectById(id);
            if (voucherWord.getIsDefault() == 1){
                throw new Exception("默认凭证字不能删除");
            }
        }
        return voucherWordMapper.deleteVoucherWordByIds(ids);
    }

    /**
     * 删除凭证字信息
     * 
     * @param id 凭证字主键
     * @return 结果
     */
    @Override
    public int deleteVoucherWordById(Long id) throws Exception {
        VoucherWord voucherWord = voucherWordMapper.selectVoucherWordById(id);
        if (voucherWord.getIsDefault() == 1){
            throw new Exception("默认凭证字不能删除");
        }
        return voucherWordMapper.deleteVoucherWordById(id);
    }
}
